home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / me310.zip / UE310C.ZIP / INPUT.C < prev    next >
C/C++ Source or Header  |  1989-10-24  |  20KB  |  897 lines

  1. /*    Input:    Various input routines for MicroEMACS
  2.         written by Daniel Lawrence
  3.         5/9/86                        */
  4.  
  5. /*
  6.     Notes:
  7.  
  8.     MicroEMACS's kernel processes two distinct forms of
  9.     characters.  One of these is a standard unsigned character
  10.     which is used in the edited text.  The other form, called
  11.     an EMACS Extended Character is a 2 byte value which contains
  12.     both an ascii value, and flags for certain prefixes/events.
  13.  
  14.     Bit    Usage
  15.     ---    -----
  16.     0 = 7    Standard 8 bit ascii character
  17.     8    Control key flag
  18.     9    META prefix flag
  19.     10    ^X prefix flag
  20.     11    Function key flag
  21.     12    Mouse prefix
  22.      13    Shifted flag (not needed on alpha shifted characters)
  23.     14    Alterate prefix (ALT key on PCs)
  24.  
  25.     The machine dependent driver is responsible for returning
  26.     a byte stream from the various input devices with various
  27.     prefixes/events embedded as escape codes.  Zero is used as the
  28.     value indicating an escape sequence is next.  The format of
  29.     an escape sequence is as follows:
  30.  
  31.     0        Escape indicator
  32.     <prefix byte>    upper byte of extended character
  33.     {<col><row>}    col, row position if the prefix byte
  34.             indicated a mouse event
  35.     <event code>    value of event
  36.  
  37.     Two successive zeroes are used to indicate an actual
  38.     null being input.  These values are then interpreted by
  39.     getkey() to construct the proper extended character
  40.     sequences to pass to the MicroEMACS kernel.
  41.  
  42.  
  43.     14-oct-1989 Sugih Jamin:
  44.     Fixed complete() so that it keeps around the path of the last file
  45.     found.  This path is displayed the next time complete is called with
  46.     argument CMP_FILENAME.
  47.  
  48.     Added a new case to complete() to handle pattern matching.  Now ENTER
  49.     could be used to end a search pattern.  META still works as before.
  50.     To insert a literal ENTER, use CTRL-Q to quote it first.
  51.     Also, space at the beginning of the command line will spit out the last
  52.     pattern saved, the user can then edit this pattern.
  53. */
  54.  
  55. #include    <stdio.h>
  56. #include    "estruct.h"
  57. #include    "etype.h"
  58. #include    "edef.h"
  59. #include    "elang.h"
  60.  
  61. static char cwd[NFILEN]="";    /* current working directory, initially null */
  62. /*
  63.  * Ask a yes or no question in the message line. Return either TRUE, FALSE, or
  64.  * ABORT. The ABORT status is returned if the user bumps out of the question
  65.  * with a ^G. Used any time a confirmation is required.
  66.  */
  67.  
  68. PASCAL NEAR mlyesno(prompt)
  69.  
  70. char *prompt;
  71.  
  72. {
  73.     int  c;            /* input character */
  74.     char buf[NPAT];        /* prompt to user */
  75.  
  76.     for (;;) {
  77.         /* build and prompt the user */
  78.         strcpy(buf, prompt);
  79.         strcat(buf, TEXT162);
  80. /*                          " [y/n]? " */
  81.         mlwrite(buf);
  82.  
  83.         /* get the response */
  84.             c = getcmd();   /* getcmd() lets us check for anything that might */
  85.                             /* generate a 'y' or 'Y' in case use screws up */
  86.  
  87.         if (c == ectoc(abortc))        /* Bail out! */
  88.             return(ABORT);
  89.  
  90.             if  ((c == 'n') || (c == 'N')
  91.                 || (c & (SPEC|ALTD|CTRL|META|CTLX|MOUS)))
  92.                     return(FALSE);  /* ONLY 'y' or 'Y' allowed!!! */
  93.  
  94. #if    FRENCH
  95.         if (c=='o' || c=='O')
  96.             return(TRUE);
  97. #endif
  98.  
  99.         if (c=='y' || c=='Y')
  100.             return(TRUE);
  101.  
  102.         return(FALSE);
  103.     }
  104. }
  105.  
  106. /*
  107.  * Write a prompt into the message line, then read back a response. Keep
  108.  * track of the physical position of the cursor. If we are in a keyboard
  109.  * macro throw the prompt away, and return the remembered response. This
  110.  * lets macros run at full speed. The reply is always terminated by a carriage
  111.  * return. Handle erase, kill, and abort keys.
  112.  */
  113.  
  114. PASCAL NEAR mlreply(prompt, buf, nbuf)
  115.     char *prompt;
  116.     char *buf;
  117. {
  118.     return(nextarg(prompt, buf, nbuf, ctoec((int) '\r')));
  119. }
  120.  
  121. PASCAL NEAR mltreply(prompt, buf, nbuf, eolchar)
  122.  
  123. char *prompt;
  124. char *buf;
  125. int nbuf;
  126. int eolchar;
  127.  
  128. {
  129.     return(nextarg(prompt, buf, nbuf, eolchar));
  130. }
  131.  
  132. /*    ectoc:    expanded character to character
  133.         collapse the CTRL and SPEC flags back into an ascii code   */
  134.  
  135. PASCAL NEAR ectoc(c)
  136.  
  137. int c;
  138.  
  139. {
  140.     if (c & CTRL)
  141.         c = c & ~(CTRL | 0x40);
  142.     if (c & SPEC)
  143.         c= c & 255;
  144.     return(c);
  145. }
  146.  
  147. /*    ctoec:    character to extended character
  148.         pull out the CTRL and SPEC prefixes (if possible)    */
  149.  
  150. PASCAL NEAR ctoec(c)
  151.  
  152. int c;
  153.  
  154. {
  155.         if (c>=0x00 && c<=0x1F)
  156.                 c = CTRL | (c+'@');
  157.         return(c);
  158. }
  159.  
  160. /* get a command name from the command line. Command completion means
  161.    that pressing a <SPACE> will attempt to complete an unfinished command
  162.    name if it is unique.
  163. */
  164.  
  165. int (PASCAL NEAR *PASCAL NEAR getname(prompt))()
  166.  
  167. char *prompt;    /* string to prompt with */
  168.  
  169. {
  170.     char *sp;    /* ptr to the returned string */
  171.  
  172.     sp = complete(prompt, NULL, CMP_COMMAND, NSTRING);
  173.     if (sp == NULL)
  174.         return(NULL);
  175.  
  176.     return(fncmatch(sp));
  177. }
  178.  
  179. /*    getcbuf:    get a completion from the user for a buffer name.
  180.  
  181.             I was goaded into this by lots of other people's
  182.             completion code.
  183. */
  184.  
  185. BUFFER *PASCAL NEAR getcbuf(prompt, defval, createflag)
  186.  
  187. char *prompt;        /* prompt to user on command line */
  188. char *defval;        /* default value to display to user */
  189. int createflag;        /* should this create a new buffer? */
  190.  
  191. {
  192.     char *sp;    /* ptr to the returned string */
  193.  
  194.     sp = complete(prompt, defval, CMP_BUFFER, NBUFN);
  195.     if (sp == NULL)
  196.         return(NULL);
  197.  
  198.     return(bfind(sp, createflag, 0));
  199. }
  200.  
  201. char *PASCAL NEAR gtfilename(prompt)
  202.  
  203. char *prompt;        /* prompt to user on command line */
  204.  
  205. {
  206.     char *sp;    /* ptr to the returned string */
  207.  
  208.     sp = complete(prompt, NULL, CMP_FILENAME, NFILEN);
  209.     if (sp == NULL)
  210.         return(NULL);
  211.     return(sp);
  212. }
  213.  
  214. int
  215. gtpattern(prompt, buf, pattern, length)
  216.     char *prompt;
  217.     char *buf;
  218.     char *pattern;
  219.     int length;
  220. {
  221.     char *sp;
  222.     sp = complete(prompt, pattern, CMP_PATTERN, length);
  223.  
  224.     if (!sp) {
  225.         return(ABORT);
  226.     }
  227.     strcpy(buf,sp);
  228.  
  229.     return(TRUE);
  230. }
  231.  
  232. char *PASCAL NEAR
  233. complete(prompt, defval, type, maxlen)
  234.  
  235. char *prompt;        /* prompt to user on command line */
  236. char *defval;        /* default value to display to user */
  237. int type;        /* type of what we are completing */
  238. int maxlen;        /* maximum length of input field */
  239.  
  240. {
  241.     register int c;        /* current input character */
  242.     int cpos;        /* current column on screen output */
  243.     static char buf[NSTRING];/* buffer to hold tentative name */
  244.     int quotef = FALSE;        /* quote next char, except abort */
  245. #if    COMPLET == 0
  246.     int status;
  247. #endif
  248.  
  249.     /* if we are executing a command line get the next arg and match it */
  250.     if (clexec) {
  251.         if (macarg(buf) != TRUE)
  252.             return(NULL);
  253.         return(buf);
  254.     }
  255.  
  256. #if    COMPLET == 0
  257.     strcpy(buf, prompt);
  258.     if (type != CMP_COMMAND) {
  259.         if (defval) {
  260.             strcat(buf, "[");
  261.             strcat(buf, defval);
  262.             strcat(buf, "]: ");
  263.         } else {
  264.             strcat(buf, ": ");
  265.         }
  266.     }
  267.     status = mlreply(buf, buf, maxlen);
  268.     if (status == ABORT)
  269.         return(NULL);
  270.     if (defval && (buf[0] == 0))
  271.         return(defval);
  272.     return(buf);
  273. }
  274. #else
  275.     /* starting at the end of the string buffer */
  276.     cpos = 0;
  277.  
  278.     /* if it exists, prompt the user for a buffer name */
  279.     if (prompt)
  280.         if (type == CMP_COMMAND) {
  281.             mlwrite("%s", prompt);
  282.         } else if (defval) {
  283.             mlwrite("%s[%s]: ", prompt, defval);
  284.         } else if (type == CMP_FILENAME) {
  285.             strcpy(buf,cwd);
  286.             cpos += strlen(cwd);
  287.             mlwrite("%s: %s", prompt, buf);
  288.         } else {
  289.             mlwrite("%s: ", prompt);
  290.         }
  291.  
  292.     /* build a name string from the keyboard */
  293.     while (TRUE) {
  294.         c = tgetc();
  295.  
  296.         /* if we are at the end, just match it; but, see quote char below */
  297.         if (!quotef && (c == '\n'  ||  c == '\r'|| (c == ectoc(sterm)))) {
  298.             if (defval && cpos==0) {
  299.                 return(defval);
  300.             } else {
  301.                 buf[cpos] = 0;
  302.                 
  303.                 if (type == CMP_FILENAME) {
  304.                     while (!(cpos < 0
  305.                             || buf[cpos] == '/'
  306.                             || buf[cpos] == '\\'
  307.                             || buf[cpos] == ':'))
  308.                     {
  309.                         cpos--;
  310.                     }
  311.                     if (cpos >= 0) {
  312.                         strncpy(cwd,buf,++cpos);
  313.                         cwd[cpos]='\0';
  314.                     } else {
  315.                         cwd[0] = 0;
  316.                     }
  317.                 }
  318.                 
  319.                 return(buf);
  320.             }
  321.  
  322.         } else if (c == ectoc(abortc)) {    /* Bell, abort */
  323.             ctrlg(FALSE, 0);
  324.             TTflush();
  325.             return(NULL);
  326.  
  327.         } else if (!quotef && (c == 0x7F || c == 0x08)) {    /* rubout/erase */
  328.             if (cpos != 0) {
  329.                 --cpos;
  330.                 if (buf[cpos] < ' ' && cpos > 1) {
  331.                     mlout('\b');
  332.                     mlout(' ');
  333.                     mlout('\b');
  334.                     --ttcol;
  335.                 }
  336.                 mlout('\b');
  337.                 mlout(' ');
  338.                 mlout('\b');
  339.                 --ttcol;
  340.                 TTflush();
  341.             }
  342.  
  343.         } else if (!quotef && (c == 0x15)) {    /* C-U, kill */
  344.             if (cpos == 0 && defval) {
  345.                 defval = "";
  346.                 mlwrite("%s[]: ", prompt);
  347.             } else {
  348.                 while (cpos != 0) {
  349.                     --cpos;
  350.                     if (buf[cpos] < ' ') {
  351.                         mlout('\b');
  352.                         mlout(' ');
  353.                         mlout('\b');
  354.                         --ttcol;
  355.                     }
  356.                     mlout('\b');
  357.                     mlout(' ');
  358.                     mlout('\b');
  359.                     --ttcol;
  360.                 }
  361.             }
  362.             TTflush();
  363.  
  364.         } else if (!quotef && ((c == ' ') || (c == '\t'))) {    
  365.             /* attempt a completion */
  366.             switch (type) {
  367.                 case CMP_BUFFER:
  368.                     comp_buffer(buf, &cpos);
  369.                     break;
  370.                 case CMP_COMMAND:
  371.                     comp_command(buf, &cpos);
  372.                     break;
  373.                 case CMP_FILENAME:
  374.                     comp_file(buf, &cpos);
  375.                     break;
  376.                 case CMP_PATTERN:
  377.                     if (!cpos && defval) {
  378.                         int i;
  379.                         
  380.                         strcpy(buf,defval);
  381.                         cpos = strlen(defval);
  382.                         for (i = 0; i < cpos; i++) {
  383.                             TTputc(defval[i]);
  384.                         }
  385.                     } else if (cpos < maxlen) {
  386.                         buf[cpos++] = c;
  387.                         mlout(c);
  388.                         ++ttcol;
  389.                     }                        
  390.             }
  391.  
  392.             TTflush();
  393.             if (buf[cpos - 1] == 0)
  394.                 return(buf);
  395.         } else if (c == quotec) { /* quote next character */
  396.             quotef = TRUE;
  397.         } else if (cpos < maxlen) {
  398.             if (quotef && c < ' ') {
  399.                 quotef = FALSE;
  400.                 mlout('^');
  401.                 mlout(c ^ 0x40);
  402.                 ttcol += 2;
  403.                 buf[cpos++] = c;
  404.                 TTflush();
  405.             } else if (!(c < ' ')) {
  406.                 mlout(c);
  407.                 ++ttcol;
  408.                 buf[cpos++] = c;
  409.                 TTflush();
  410.             }
  411.         }
  412.     }
  413. }
  414.  
  415. /*    comp_command:    Attempt a completion on a command name    */
  416.  
  417. comp_command(name, cpos)
  418.  
  419. char *name;    /* command containing the current name to complete */
  420. int *cpos;    /* ptr to position of next character to insert */
  421.  
  422. {
  423.     register NBIND *bp;    /* trial command to complete */
  424.     register int index;    /* index into strings to compare */
  425.     register int curbind;    /* index into the names[] array */
  426.     register NBIND *match;    /* last command that matches string */
  427.     register int matchflag;    /* did this command name match? */
  428.     register int comflag;    /* was there a completion at all? */
  429.  
  430.     /* start attempting completions, one character at a time */
  431.     comflag = FALSE;
  432.     curbind = 0;
  433.     while (*cpos < NSTRING) {
  434.  
  435.         /* first, we start at the first command and scan the list */
  436.         match = NULL;
  437.         curbind = 0;
  438.         while (curbind <= numfunc) {
  439.  
  440.             /* is this a match? */
  441.             bp = &names[curbind];
  442.             matchflag = TRUE;
  443.             for (index = 0; index < *cpos; index++)
  444.                 if (name[index] != bp->n_name[index]) {
  445.                     matchflag = FALSE;
  446.                     break;
  447.                 }
  448.  
  449.             /* if it is a match */
  450.             if (matchflag) {
  451.  
  452.                 /* if this is the first match, simply record it */
  453.                 if (match == NULL) {
  454.                     match = bp;
  455.                     name[*cpos] = bp->n_name[*cpos];
  456.                 } else {
  457.                     /* if there's a difference, stop here */
  458.                     if (name[*cpos] != bp->n_name[*cpos])
  459.                         return;
  460.                 }
  461.             }
  462.  
  463.             /* on to the next command */
  464.             curbind++;
  465.         }
  466.  
  467.         /* with no match, we are done */
  468.         if (match == NULL) {
  469.             /* beep if we never matched */
  470.             if (comflag == FALSE)
  471.                 TTbeep();
  472.             return;
  473.         }
  474.  
  475.         /* if we have completed all the way... go back */
  476.         if (name[*cpos] == 0) {
  477.             (*cpos)++;
  478.             return;
  479.         }
  480.  
  481.         /* remember we matched, and complete one character */
  482.         comflag = TRUE;
  483.         TTputc(name[(*cpos)++]);
  484.         TTflush();
  485.     }
  486.  
  487.     /* don't allow a completion past the end of the max command name length */
  488.     return;
  489. }
  490.  
  491. /*    comp_buffer:    Attempt a completion on a buffer name    */
  492.  
  493. comp_buffer(name, cpos)
  494.  
  495. char *name;    /* buffer containing the current name to complete */
  496. int *cpos;    /* ptr to position of next character to insert */
  497.  
  498. {
  499.     register BUFFER *bp;    /* trial buffer to complete */
  500.     register int index;    /* index into strings to compare */
  501.     register BUFFER *match;    /* last buffer that matches string */
  502.     register int matchflag;    /* did this buffer name match? */
  503.     register int comflag;    /* was there a completion at all? */
  504.  
  505.     /* start attempting completions, one character at a time */
  506.     comflag = FALSE;
  507.     while (*cpos < NBUFN) {
  508.  
  509.         /* first, we start at the first buffer and scan the list */
  510.         match = NULL;
  511.         bp = bheadp;
  512.         while (bp) {
  513.  
  514.             /* is this a match? */
  515.             matchflag = TRUE;
  516.             for (index = 0; index < *cpos; index++)
  517.                 if (name[index] != bp->b_bname[index]) {
  518.                     matchflag = FALSE;
  519.                     break;
  520.                 }
  521.  
  522.             /* if it is a match */
  523.             if (matchflag) {
  524.  
  525.                 /* if this is the first match, simply record it */
  526.                 if (match == NULL) {
  527.                     match = bp;
  528.                     name[*cpos] = bp->b_bname[*cpos];
  529.                 } else {
  530.                     /* if there's a difference, stop here */
  531.                     if (name[*cpos] != bp->b_bname[*cpos])
  532.                         return;
  533.                 }
  534.             }
  535.  
  536.             /* on to the next buffer */
  537.             bp = bp->b_bufp;
  538.         }
  539.  
  540.         /* with no match, we are done */
  541.         if (match == NULL) {
  542.             /* beep if we never matched */
  543.             if (comflag == FALSE)
  544.                 TTbeep();
  545.             return;
  546.         }
  547.  
  548.         /* if we have completed all the way... go back */
  549.         if (name[*cpos] == 0) {
  550.             (*cpos)++;
  551.             return;
  552.         }
  553.  
  554.         /* remember we matched, and complete one character */
  555.         comflag = TRUE;
  556.         TTputc(name[(*cpos)++]);
  557.         TTflush();
  558.     }
  559.  
  560.     /* don't allow a completion past the end of the max buffer name length */
  561.     return;
  562. }
  563.  
  564. /*    comp_file:    Attempt a completion on a file name    */
  565.  
  566. comp_file(name, cpos)
  567.  
  568. char *name;    /* file containing the current name to complete */
  569. int *cpos;    /* ptr to position of next character to insert */
  570.  
  571. {
  572.     register char *fname;    /* trial file to complete */
  573.     register int index;    /* index into strings to compare */
  574.     register char *match;    /* last file that matches string */
  575.     register int matchflag;    /* did this file name match? */
  576.     register int comflag;    /* was there a completion at all? */
  577.  
  578.     /* start attempting completions, one character at a time */
  579.     comflag = FALSE;
  580.     while (*cpos < NBUFN) {
  581.  
  582.         /* first, we start at the first file and scan the list */
  583.         match = NULL;
  584.         name[*cpos] = 0;
  585.         fname = getffile(name);
  586.         while (fname) {
  587.  
  588.             /* is this a match? */
  589.             matchflag = TRUE;
  590.             for (index = 0; index < *cpos; index++)
  591.                 if (name[index] != fname[index]) {
  592.                     matchflag = FALSE;
  593.                     break;
  594.                 }
  595.  
  596.             /* if it is a match */
  597.             if (matchflag && index) {
  598.  
  599.                 /* if this is the first match, simply record it */
  600.                 if (match == NULL) {
  601.                     match = fname;
  602.                     name[*cpos] = fname[*cpos];
  603.                 } else {
  604.                     /* if there's a difference, stop here */
  605.                     if (name[*cpos] != fname[*cpos])
  606.                         return;
  607.                 }
  608.             }
  609.  
  610.             /* on to the next file */
  611.             fname = getnfile();
  612.         }
  613.  
  614.         /* with no match, we are done */
  615.         if (match == NULL) {
  616.             /* beep if we never matched */
  617.             if (comflag == FALSE)
  618.                 TTbeep();
  619.             return;
  620.         }
  621.  
  622.         /* if we have completed all the way... go back */
  623.         if (name[*cpos] == 0) {
  624.             (*cpos)++;
  625.             return;
  626.         }
  627.  
  628.         /* remember we matched, and complete one character */
  629.         comflag = TRUE;
  630.         TTputc(name[(*cpos)++]);
  631.         TTflush();
  632.     }
  633.  
  634.     /* don't allow a completion past the end of the max file name length */
  635.     return;
  636. }
  637. #endif
  638.  
  639. /*    tgetc:    Get a key from the terminal driver, resolve any keyboard
  640.         macro action                    */
  641.  
  642. int PASCAL NEAR tgetc()
  643.  
  644. {
  645.     int c;    /* fetched character */
  646.  
  647.     /* if we are playing a keyboard macro back, */
  648.     if (kbdmode == PLAY) {
  649.  
  650.         /* if there is some left... */
  651.         if (kbdptr < kbdend)
  652.             return((int)*kbdptr++);
  653.  
  654.         /* at the end of last repitition? */
  655.         if (--kbdrep < 1) {
  656.             kbdmode = STOP;
  657. #if    VISMAC == 0
  658.             /* force a screen update after all is done */
  659.             update(FALSE);
  660. #endif
  661.         } else {
  662.  
  663.             /* reset the macro to the begining for the next rep */
  664.             kbdptr = &kbdm[0];
  665.             return((int)*kbdptr++);
  666.         }
  667.     }
  668.  
  669.     /* fetch a character from the terminal driver */
  670.     c = TTgetc();
  671.  
  672.     /* record it for $lastkey */
  673.     lastkey = c;
  674.  
  675.     /* save it if we need to */
  676.     if (kbdmode == RECORD) {
  677.         *kbdptr++ = c;
  678.         kbdend = kbdptr;
  679.  
  680.         /* don't overrun the buffer */
  681.         if (kbdptr == &kbdm[NKBDM - 1]) {
  682.             kbdmode = STOP;
  683.             TTbeep();
  684.         }
  685.     }
  686.  
  687.     /* and finally give the char back */
  688.     return(c);
  689. }
  690.  
  691. /*    getkey:    Get one keystroke. The only prefixs legal here
  692.             are the SPEC and CTRL prefixes.
  693. */
  694.  
  695. PASCAL NEAR getkey()
  696.  
  697. {
  698.     int c;        /* next input character */
  699.     int upper;    /* upper byte of the extended sequence */
  700.  
  701.     /* get a keystroke */
  702.         c = tgetc();
  703.  
  704.     /* if it exists, process an escape sequence */
  705.     if (c == 0) {
  706.  
  707.         /* get the event type */
  708.         upper = tgetc();
  709.  
  710.         /* mouse events need us to read in the row/col */
  711.         if (upper & (MOUS >> 8)) {
  712.             /* grab the x/y position of the mouse */
  713.             xpos = tgetc();
  714.             ypos = tgetc();
  715.         }
  716.  
  717.         /* get the event code */
  718.         c = tgetc();
  719.  
  720.         /* if it is a function key... map it */
  721. #if    0    /* this should be here...fix it for MSDOS dan! */
  722.         if (upper & (SPEC >> 8))
  723.             c = (upper << 8) | extcode(c & 255);
  724.         else
  725. #endif
  726.             c = (upper << 8) | c;
  727.  
  728.     }
  729.  
  730.     /* yank out the control prefix */
  731.         if ((c & 255) >=0x00 && (c & 255) <= 0x1F)
  732.                 c = CTRL | (c+'@');
  733.  
  734.     /* return the character */
  735.         return(c);
  736. }
  737.  
  738. /*    GETCMD:    Get a command from the keyboard. Process all applicable
  739.         prefix keys
  740.                             */
  741. PASCAL NEAR getcmd()
  742.  
  743. {
  744.     int c;        /* fetched keystroke */
  745.     KEYTAB *key;    /* ptr to a key entry */
  746.  
  747.     /* get initial character */
  748.     c = getkey();
  749.     key = getbind(c);
  750.  
  751.     /* resolve META and CTLX prefixes */
  752.     if (key) {
  753.         if (key->k_ptr.fp == meta) {
  754.             c = getkey();
  755.             c = upperc(c) | (c & ~255);    /* Force to upper */
  756.             c |= META;
  757.         } else if (key->k_ptr.fp == cex) {
  758.             c = getkey();
  759.             c = upperc(c) | (c & ~255);    /* Force to upper */
  760.             c |= CTLX;
  761.         }
  762.     }
  763.  
  764.     /* return it */
  765.     return(c);
  766. }
  767.  
  768. /*    A more generalized prompt/reply function allowing the caller
  769.     to specify the proper terminator. If the terminator is not
  770.     a return('\r'), return will echo as "<NL>"
  771.                             */
  772. PASCAL NEAR getstring(prompt, buf, nbuf, eolchar)
  773.  
  774. char *prompt;
  775. char *buf;
  776. int eolchar;
  777.  
  778. {
  779.     register int cpos;    /* current character position in string */
  780.     register int c;        /* current input character */
  781.     register int quotef;    /* are we quoting the next char? */
  782.  
  783.     cpos = 0;
  784.     quotef = FALSE;
  785.  
  786.     /* prompt the user for the input string */
  787.     if (discmd)
  788.         mlwrite(prompt);
  789.     else
  790.         movecursor(term.t_nrow, 0);
  791.  
  792.     for (;;) {
  793.         /* get a character from the user */
  794.         c = getkey();
  795.  
  796.         /* if they hit the line terminate, wrap it up */
  797.         if (c == eolchar && quotef == FALSE) {
  798.             buf[cpos++] = 0;
  799.  
  800.             /* clear the message line */
  801.             mlwrite("");
  802.             TTflush();
  803.  
  804.             /* if we default the buffer, return FALSE */
  805.             if (buf[0] == 0)
  806.                 return(FALSE);
  807.  
  808.             return(TRUE);
  809.         }
  810.  
  811.         /* change from command form back to character form */
  812.         c = ectoc(c);
  813.  
  814.         if (c == ectoc(abortc) && quotef == FALSE) {
  815.             /* Abort the input? */
  816.             ctrlg(FALSE, 0);
  817.             TTflush();
  818.             return(ABORT);
  819.         } else if ((c==0x7F || c==0x08) && quotef==FALSE) {
  820.             /* rubout/erase */
  821.             if (cpos != 0) {
  822.                 outstring("\b \b");
  823.                 --ttcol;
  824.  
  825.                 if (buf[--cpos] < 0x20) {
  826.                     outstring("\b \b");
  827.                     --ttcol;
  828.                 }
  829.  
  830.                 if (buf[cpos] == '\r') {
  831.                     outstring("\b\b  \b\b");
  832.                     ttcol -= 2;
  833.                 }
  834.                 TTflush();
  835.             }
  836.  
  837.         } else if (c == 0x15 && quotef == FALSE) {
  838.             /* C-U, kill */
  839.             while (cpos != 0) {
  840.                 outstring("\b \b");
  841.                 --ttcol;
  842.  
  843.                 if (buf[--cpos] < 0x20) {
  844.                     outstring("\b \b");
  845.                     --ttcol;
  846.                 }
  847.             }
  848.             TTflush();
  849.  
  850.         } else if (c == quotec && quotef == FALSE) {
  851.             quotef = TRUE;
  852.         } else {
  853.             quotef = FALSE;
  854.             if (cpos < nbuf-1) {
  855.                 buf[cpos++] = c;
  856.  
  857.                 if ((c < ' ') && (c != '\r')) {
  858.                     outstring("^");
  859.                     ++ttcol;
  860.                     c ^= 0x40;
  861.                 }
  862.  
  863.                 if (c != '\r') {
  864.                     if (disinp)
  865.                         mlout(c);
  866.                 } else {    /* put out <NL> for <ret> */
  867.                     outstring("<NL>");
  868.                     ttcol += 3;
  869.                 }
  870.                 ++ttcol;
  871.                 TTflush();
  872.             }
  873.         }
  874.     }
  875. }
  876.  
  877. PASCAL NEAR outstring(s) /* output a string of input characters */
  878.  
  879. char *s;    /* string to output */
  880.  
  881. {
  882.     if (disinp)
  883.         while (*s)
  884.             mlout(*s++);
  885. }
  886.  
  887. PASCAL NEAR ostring(s)    /* output a string of output characters */
  888.  
  889. char *s;    /* string to output */
  890.  
  891. {
  892.     if (discmd)
  893.         while (*s)
  894.             mlout(*s++);
  895. }
  896.  
  897.